//
//  QSSearchNavgationBar.m
//  QSSmarkPark-iOS
//
//  Created by jxsoft on 2018/7/26.
//  Copyright © 2018年 QiSheng. All rights reserved.
//

#import "QSHomeNavgationBar.h"

@interface QSHomeNavgationBar ()

@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIImageView *searchImageView;
@property (nonatomic, strong) UIButton *historyButton;

@end

@implementation QSHomeNavgationBar

#pragma mark - **************** Initials
- (instancetype)init {
    return [self initWithFrame:CGRectMake(0, 0, kScreenWidth,kNavgationBarHeight)];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self loadUI];
    }
    return self;
}

- (void)loadUI {
    [self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self);
        make.top.equalTo(self);
        make.bottom.equalTo(self);
    }];
    
    [self.searchBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(kRealValue(15));
        make.top.equalTo(self).offset(kStatusBarHeight + 7);
        make.right.equalTo(self).offset(-kRealValue(50));
        make.height.equalTo(@30);
    }];
    
    [self.searchImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.searchBarView);
        make.right.equalTo(self.searchBarView).offset(-kRealValue(10));
        make.size.mas_equalTo(CGSizeMake(kRealValue(22), kRealValue(22)));
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.searchBarView).offset(kRealValue(15));
        make.centerY.equalTo(self.searchBarView);
        make.height.equalTo(@kRealValue(14));
        make.right.equalTo(self.searchImageView.mas_left).offset(-kRealValue(15));
    }];
    
    [self.historyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.searchBarView);
        make.right.equalTo(self);
        make.left.equalTo(self.searchBarView.mas_right);
        make.height.equalTo(@44);
    }];
}

#pragma mark - **************** Event Response
- (void)turnToHistoryView {
    if (self.homeNavgationBarTurnToHistoryViewBlock) {
        self.homeNavgationBarTurnToHistoryViewBlock();
    }
}

- (void)turnToSearchView {
    if (self.homeNavgationBarTurnToSearchViewBlock) {
        self.homeNavgationBarTurnToSearchViewBlock();
    }
}

#pragma mark - **************** Setter Getter
- (UIView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc] init];
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.frame = CGRectMake(0, 0, kScreenWidth, kNavgationBarHeight);
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(1, 1);
        gradientLayer.colors = @[(__bridge id)[UIColor QSColorBlue3DC4FC].CGColor,
                                 (__bridge id)[UIColor QSColorBlue3569FF].CGColor];
        gradientLayer.locations = @[@(0.5f), @(1.0f)];
        [_backgroundView.layer addSublayer:gradientLayer];
        _backgroundView.alpha = 0;
        [self addSubview:_backgroundView];
    }
    return _backgroundView;
}

- (UIView *)searchBarView {
    if (!_searchBarView) {
        _searchBarView = [[UIView alloc] init];
        _searchBarView.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
        _searchBarView.layer.cornerRadius = kRealValue(15);
        _searchBarView.layer.masksToBounds = YES;
        _searchBarView.layer.borderWidth = kRealValue(1);
        _searchBarView.layer.borderColor = [UIColor clearColor].CGColor;
        UITapGestureRecognizer *click = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(turnToSearchView)];
        [_searchBarView addGestureRecognizer:click];
        [self addSubview:_searchBarView];
    }
    return _searchBarView;
}

- (UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.textColor = [UIColor QSColorGrayBBBEC4];
        _contentLabel.font = [UIFont QSFontOfSize14];
        [self.searchBarView addSubview:_contentLabel];
    }
    return _contentLabel;
}

- (UIImageView *)searchImageView {
    if (!_searchImageView) {
        _searchImageView = [[UIImageView alloc] init];
        [_searchImageView setImage:[UIImage imageNamed:@"icon_home_nav_search"]];
        [self.searchBarView addSubview:_searchImageView];
    }
    return _searchImageView;
}

- (UIButton *)historyButton {
    if (!_historyButton) {
        _historyButton = [[UIButton alloc] init];
        [_historyButton setImage:[UIImage imageNamed:@"ic_home_message"] forState:UIControlStateNormal];
        [_historyButton addTarget:self action:@selector(turnToHistoryView) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_historyButton];
    }
    return _historyButton;
}

- (void)setContent:(NSString *)content {
    _content = content;
    self.contentLabel.text = content;
}

- (void)setRightButtonImage:(UIImage *)rightButtonImage {
    _rightButtonImage = rightButtonImage;
    [_historyButton setImage:rightButtonImage forState:UIControlStateNormal];
}

@end
